<?php
	/**
	 * 获取战绩列表
	 */
    class CombatGains extends APIBase {
		
		public $tag = "CombatGains";
    	public $isLogin = false;
		
		public function before() {
			$this->initCacheRedis();
			$this->initMysql();
			return true;
		}
		public function logic() {
			
			//$ann = $this->cache_redis->hMGet("ha:*", array("id","title","content","image_url"));
			//$combatgains= $this->cache_redis->keys("hCombatGains:*");
			$userid =  $this->param['playerID'];
			$UserCombatgains = $this->cache_redis->keys("hUserCombatgains:$userid:*");
			if(!empty($UserCombatgains)){
				$count = count($UserCombatgains);
				$result = array();
			    if ($count) {
				    for($i=0;$i<$count;$i++){
					 $result[] = $this->cache_redis->hGetAll("hUserCombatgains:$userid:".$i); 
				    }
	            }
				$this->returnData($result);
			}else{
				//获取条件
				$sql = "select gameID,deskno,deskBuildDate,roomid from customgamelog where playerID = $userid group by deskBuildDate";
			    $result = $this->mysql->query($sql);
				$rows = array();
				if($result){
					while($row = mysql_fetch_assoc($result)){
						$rows[] = $row;
					}
					if($rows){
						$arr = array();
						for($i=0;$i<count($rows);$i++){
							$gameID = $rows[$i]['gameID'];
							$deskno = $rows[$i]['deskno'];
							$deskBuildDate = $rows[$i]['deskBuildDate'];
							$roomid = $rows[$i]['roomid'];
							$sql_0 = "select gameID,deskno,ownerID,buildDate,deskCode,playerGameInfo,roomid from customroomrecord where gameID=$gameID and deskno=$deskno and buildDate=$deskBuildDate and roomid=$roomid";
							$list = $this->mysql->query($sql_0);
							if($list){
								$lists= mysql_fetch_assoc($list);
								$r = array();
								if($lists['playerGameInfo']!=""){
		                            $r['gameID'] = (int)$lists['gameID'];
		                            $r['deskno'] = (int)$lists['deskno'];
		                            $r['deskCode'] = (int)$lists['deskCode'];
		                            $r['buildDate'] = (int)$lists['buildDate'];
		                            $r['ownerID'] = (int)$lists['ownerID'];
									$r['roomid'] = (int)$lists['roomid'];
		                            $r['playerGameInfo'] = (string)$lists['playerGameInfo'];
								    $arr[] = $r;
							    }					
							}
						}
						if(!empty($arr)){
						    foreach($arr as $key=>$val){
							    $this->cache_redis->hMset("hUserCombatgains:$userid:{$key}",$val);
						    }
						    $this->returnData($arr);
						}else{
							$this->returnError(302,"对不起，你尚未有战绩!");
						}
					}else{
					$this->returnError(303,"对不起，你尚未游戏!");
				   }
				}
	
			}

		}
		
        public function after() {
			$this->deinitCacheRedis();
		}
    }
?>
